﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Threading;
using DreamIn.GUI;
using DreamIn.Structures;
using DreamIn.Structures.Virtualization;
using DreamIn.Structures.Utils;

namespace DreamIn
{
    /// <summary>
    /// Interaction logic for Carousel.xaml
    /// </summary>
    public partial class Carousel : UserControl
    {
        private List<Picture> _pictureList;
        private List<Album> _albumList;
        private Thread _loadingThread;
        private Album _pictureAlbum;
        private struct ThreadParams
        {
            public ThreadParams(Boolean reset, LoadType type)
            {
                this.reset = reset;
                this.type = type;
            }
            public Boolean reset;
            public LoadType type;
        }
        public enum LoadType
        {
            Picture,
            Album
        }
        public Carousel()
        {
            this.InitializeComponent();
        }

        //private void LoadNextImages()
        //{
        //    flowControl.Clear();
        //    _loadingThread = new Thread(new ThreadStart(PictureWorkerThread));
        //    _loadingThread.SetApartmentState(ApartmentState.STA);
        //    _loadingThread.IsBackground = true;
        //    _loadingThread.Name = "LoadPictureWorker";
        //    _loadingThread.Start();
        //    //worker.Join();
        //}
        //private void LoadNextAlbums()
        //{
        //    flowControl.Clear();
        //    _loadingThread = new Thread(new ThreadStart(AlbumWorkerThread));
        //    _loadingThread=new Thread(new 
        //    _loadingThread.SetApartmentState(ApartmentState.STA);
        //    _loadingThread.IsBackground = true;
        //    _loadingThread.Name = "LoadAlbumWorker";

        //    _loadingThread.Start();

        //}
        private void LoadNext(ThreadParams threadParams)
        {
            if (_loadingThread != null && _loadingThread.IsAlive)
            {
                _loadingThread.Abort();
                _loadingThread.Join();
            }
            lock(flowControl)
            flowControl.Clear();
            _loadingThread = new Thread(new ParameterizedThreadStart(LoadWorkerThread));
            _loadingThread.SetApartmentState(ApartmentState.STA);
            _loadingThread.IsBackground = true;
            _loadingThread.Name = "LoadWorker";
            _loadingThread.Start(threadParams);
        }
        private void LoadWorkerThread(object thrParams)
        {
            ThreadParams threadParams = (ThreadParams)thrParams;
            LoadType loadType = threadParams.type;
            if (loadType == LoadType.Picture)
            {
                foreach (Picture picture in _pictureList)
                {
                    App.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(
                        delegate
                        {
                            BitmapImage bitmapImage = new BitmapImage();
                            bitmapImage.BeginInit();
                            bitmapImage.UriSource = new Uri(_pictureList[flowControl.Count].PicturePath, UriKind.Absolute);
                            bitmapImage.DecodePixelWidth = 245;
                            bitmapImage.DecodePixelHeight = 180;
                            bitmapImage.EndInit();
                            CarouselItem carouselItem = new CarouselItem(bitmapImage, _pictureList[flowControl.Count],_pictureAlbum);
                            carouselItem.LoadAlbums += new LoadAlbumsEventHandler(carouselItem_LoadAlbums);
                            carouselItem.Width = 245;
                            carouselItem.Height = 180;
                            flowControl.Add(carouselItem);   
                        }));
                }
            }
            else if (loadType == LoadType.Album)
            {
                foreach (Album album in _albumList)
                {
                    App.Current.Dispatcher.BeginInvoke(DispatcherPriority.DataBind, new Action(
                        delegate
                        {
                            try
                            {
                                BitmapImage bitmapImage = new BitmapImage();
                                bitmapImage.BeginInit();
                                bitmapImage.UriSource = new Uri(_albumList[flowControl.Count].Pictures[0].PicturePath, UriKind.Absolute);
                                bitmapImage.DecodePixelWidth = 245;
                                bitmapImage.DecodePixelHeight = 180;
                                bitmapImage.EndInit();
                                CarouselItem carouselItem = new CarouselItem(bitmapImage, _albumList[flowControl.Count]);
                                carouselItem.LoadPictures += new LoadPicturesEventHandler(carouselItem_LoadPictures);
                                carouselItem.Width = 245;
                                carouselItem.Height = 180;
                                flowControl.Add(carouselItem);
                            }
                            catch (Exception e)
                            {
                            }
                        }));
                }
            }

        }
        private void PictureWorkerThread()
        {
            foreach (Picture picture in _pictureList)
            {
                App.Current.Dispatcher.BeginInvoke(DispatcherPriority.ContextIdle, new Action(
                    delegate
                    {

                        BitmapImage bitmapImage = new BitmapImage();
                        bitmapImage.BeginInit();
                        bitmapImage.UriSource = new Uri(_pictureList[flowControl.Count].PicturePath, UriKind.Absolute);
                        bitmapImage.DecodePixelWidth = 245;
                        bitmapImage.DecodePixelHeight = 180;
                        bitmapImage.EndInit();
                        CarouselItem carouselItem = new CarouselItem(bitmapImage, _pictureList[flowControl.Count],_pictureAlbum);
                        carouselItem.LoadAlbums += new LoadAlbumsEventHandler(carouselItem_LoadAlbums);
                        carouselItem.Width = 245;
                        carouselItem.Height = 180;
                        flowControl.Add(carouselItem);

                    }));
            }
        }

        private void AlbumWorkerThread()
        {
            foreach (Album album in _albumList)
            {
                App.Current.Dispatcher.BeginInvoke(DispatcherPriority.ContextIdle, new Action(
                    delegate
                    {

                        BitmapImage bitmapImage = new BitmapImage();
                        bitmapImage.BeginInit();
                        bitmapImage.UriSource = new Uri(_albumList[flowControl.Count].Pictures[0].PicturePath, UriKind.Absolute);
                        bitmapImage.DecodePixelWidth = 245;
                        bitmapImage.DecodePixelHeight = 180;
                        bitmapImage.EndInit();
                        CarouselItem carouselItem = new CarouselItem(bitmapImage, _albumList[flowControl.Count]);
                        carouselItem.LoadPictures += new LoadPicturesEventHandler(carouselItem_LoadPictures);
                        carouselItem.Width = 245;
                        carouselItem.Height = 180;
                        flowControl.Add(carouselItem);


                    }));
            }
        }

        private void carouselItem_LoadPictures(Album album)
        {
            FillCarousel(album.Pictures,album);
        }

        void carouselItem_LoadAlbums()
        {
            LoadNext(new ThreadParams(true,LoadType.Album));
            // LoadNextAlbums();
        }
        public void FillCarousel(List<Picture> pictures,Album pictureAlbum)
        {
            _pictureList = pictures;
            _pictureAlbum = pictureAlbum;
            LoadNext(new ThreadParams(true,LoadType.Picture));
            //LoadNextImages();
        }
        public void FillCarousel(ThreadSafeList<Album> albums)
        {
            _albumList = new List<Album>();
            for (int i = 0; i < albums.Count; i++)
                _albumList.Add(albums[i]);
            LoadNext(new ThreadParams(true,LoadType.Album));
            // LoadNextAlbums();
        }
        public void GoToNext()
        {
            flowControl.GoToNext();
        }
        public void GoToPrevious()
        {
            flowControl.GoToPrevious();
        }
    }
}